package com.fastloan.mgt.verify.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fastloan.mgt.application.base.enums.ApplicationStatusEnum;
import com.fastloan.mgt.application.base.model.Application;
import com.fastloan.mgt.application.base.service.ApplicationService;
import com.fastloan.mgt.processcontrol.base.annotation.FunctionHandlerClass;
import com.fastloan.mgt.processcontrol.base.annotation.FunctionHandlerMethod;
import com.fastloan.mgt.processcontrol.base.model.ProcessSchedule;
import com.fastloan.mgt.processcontrol.base.model.ProcessSession;
import com.fastloan.mgt.processcontrol.base.service.ProcessDispatcher;
import com.fastloan.mgt.product.flow.enums.ProductFunctionGroup;
import com.fastloan.mgt.verify.enums.ApiStore;
import com.fastloan.mgt.verify.model.ApiResult;
import com.fastloan.mgt.verify.model.GeneralVerifyResult;
import com.fastloan.mgt.verify.model.personbank.PersonBankParam;
import com.fastloan.mgt.verify.model.personbank.VerifyCode;
import com.fastloan.mgt.verify.service.PersonBankVerifyService;
import com.fastloan.mgt.verify.service.VerifyService;
import com.fastloan.mgt.verify.util.ApiUtil;
import com.fastloan.mgt.verify.util.GetParam;
import com.kawins.result.ResultEnum;
import com.kawins.result.ResultTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

import static com.fastloan.mgt.verify.util.ApiUtil.KX_SUCCESS_CODE;

/**
 * @author yanwe
 * createTime 2017-12-2017/12/15 18:27
 */
@Service
@FunctionHandlerClass(processName = "人行征信", bmpIds = {ProductFunctionGroup.GROUP_ONE})
public class PersonBankVerifyServiceImpl implements PersonBankVerifyService{

    @Autowired
    private ApplicationService applicationService;

    @Autowired
    private VerifyService verifyService;

    @Override
    public VerifyCode getVerifyCode(){
        ApiResult apiResult = ApiUtil.sendGetSync(GetParam.newBuilder(), ApiStore.PERSON_BANK_VERIFY_GET_CODE);
        if (!Objects.equals(apiResult.getRet(), KX_SUCCESS_CODE)){
            return null;
        }
        JSONObject jsonObject = JSON.parseObject(apiResult.getData());
        return jsonObject.getObject("data",VerifyCode.class);
    }

    @Override
    @FunctionHandlerMethod
    public void personBankVerifyProcess(String processId){
        String applicationId = ProcessDispatcher.getProcessSession(processId)
                .getAttribute("applicationId", String.class);
        applicationService.modifyApplicationStatus(applicationId, ApplicationStatusEnum.PERSON_BANK_VERIFY);
    }

    @Override
    public ResultTo personBankVerify(String applicationId, PersonBankParam personBankParam){
        Application application = applicationService.selectByPrimaryKey(applicationId);
        if (!ProcessDispatcher.getCurrentProcess(application.getProcessId()).getProcessClass()
                .equalsIgnoreCase(PersonBankVerifyServiceImpl.class.getSimpleName())){
            return new ResultTo(ResultEnum.OPERATION_FAILED,"流程错误,该进件未进行到该步骤");
        }
        GeneralVerifyResult generalVerifyResult = verifyService.personBankVerify(personBankParam);
        if (!generalVerifyResult.isSuccess()){
            return new ResultTo(ResultEnum.OPERATION_FAILED,generalVerifyResult.getMsg());
        }
        ProcessSession processSession = ProcessDispatcher.getProcessSession(application.getProcessId());
        processSession.setAttribute("creditReportSessionId",personBankParam.getSessionId());
        ProcessDispatcher.executeEnd(application.getProcessId(), true);
        ProcessSchedule currentProcess = ProcessDispatcher.getCurrentProcess(application.getProcessId());
        return new ResultTo().setData("nextProcess",currentProcess);
    }
}
